/* REQUIRES EDITING */
// Edit HUD ELEMENT LOCATIONS AND HUD TEXT

init()
{
	thread players_left();
	level thread onPlayerConnect();
}

onPlayerConnect()
{
	for(;;)
	{
		level waittill("connecting", player);

		player thread onPlayerSpawned();
		player thread onPlayerKilled();
		player thread onJoinedTeam();
		player thread onJoinedSpectators();
		player thread onPlayerDisconnect();
	}
}

onJoinedTeam()
{
	self endon("disconnect");
	
	for(;;)
	{
		self waittill("joined_team");
		level notify("update_playersleft");

		self kill_display();
		wait level.frame;

		self thread setup_playerdisplay();
	}
}

onPlayerSpawned()
{
	self endon("disconnect");
	
	for(;;)
	{
		self waittill("spawned_player");
		level notify("update_playersleft");
		
		self kill_display();
		wait level.frame;

		self thread setup_playerdisplay();
	}
}

onJoinedSpectators()
{
	self endon("disconnect");
	
	for(;;)
	{
		self waittill("joined_spectators");
		level notify("update_playersleft");

		self kill_display();
		wait level.frame;

		self thread setup_specdisplay();
	}
}

onPlayerKilled()
{
	self endon("disconnect");

	for(;;)
	{
		self waittill("killed_player");

		level notify("update_playersleft");
	}
}

onPlayerDisconnect()
{
	self waittill("disconnect");
	
	level notify("update_playersleft");
}

kill_display()
{
	self notify("stop_alive_scanning");

	if (isDefined(self.teamleftnum))
		self.teamleftnum destroy();
	if (isDefined(self.teamleft))
		self.teamleft destroy();
	if (isDefined(self.enemyleftnum))
		self.enemyleftnum destroy();
	if (isDefined(self.enemyleft))
		self.enemyleft destroy();

	if (isDefined(self.axisleftnum))
		self.axisleftnum destroy();
	if (isDefined(self.axisleft))
		self.axisleft destroy();
	if (isDefined(self.alliesleftnum))
		self.alliesleftnum destroy();
	if (isDefined(self.alliesleft))
		self.alliesleft destroy();
}

setup_playerdisplay()
{
	//Debug
	if (self.name == "bot0" ||
		self.name == "bot1" ||
		self.name == "bot2" ||
		self.name == "bot3" ||
		self.name == "bot4" ||
		self.name == "bot5" ||
		self.name == "bot6" ||
		self.name == "bot7" ||
		self.name == "bot8" ||
		self.name == "bot9")
	{
		return;
	}

	if (!level.hud_playersleft)
		return;

	//if (level.gametype == "ctf" && level.hud_flagstatus)
	if (level.gametype == "ctf")
	{
		self thread setup_playerdisplay_ctf();
		return;
	}

	self endon("disconnect");
	self endon("stop_alive_scanning");

	// Team Left
	self.teamleft =  newClientHudElem(self);
	self.teamleft.x = 2;
	self.teamleft.y = 478;
	self.teamleft.alignX = "left";
	self.teamleft.alignY = "bottom";
	self.teamleft.alpha = 1;
	if (self.pers["team"] == "axis")
		self.teamleft setShader(game["plyrlft_axis"], 12, 12);
	else
		self.teamleft setShader(game["plyrlft_allies"], 12, 12);
	
	self.teamleftnum = newClientHudElem(self);
	self.teamleftnum.x = 15;
	self.teamleftnum.y = 478;
	self.teamleftnum.alignX = "left";
	self.teamleftnum.alignY = "bottom";
	self.teamleftnum.fontScale = 1.3;
	self.teamleftnum.alpha = 1;
	if (self.pers["team"] == "axis")
		self.teamleftnum.color = (1, 0.102, 0.078); // Axis Color
	else
		self.teamleftnum.color = (0.067, 0.859, 0.125); // Allied Color

		
	// Enemy Left
	self.enemyleft =  newClientHudElem(self);
	self.enemyleft.x = 95;
	self.enemyleft.y = 478;
	self.enemyleft.alignX = "left";
	self.enemyleft.alignY = "bottom";
	self.enemyleft.alpha = 1;
	if (self.pers["team"] == "allies")
		self.enemyleft setShader(game["plyrlft_axis"], 12, 12);
	else
		self.enemyleft setShader(game["plyrlft_allies"], 12, 12);

	self.enemyleftnum =  newClientHudElem(self);
	self.enemyleftnum.x = 108;
	self.enemyleftnum.y = 478;
	self.enemyleftnum.alignX = "left";
	self.enemyleftnum.alignY = "bottom";
	self.enemyleftnum.fontScale = 1.3;
	self.enemyleftnum.alpha = 1;
	if (self.pers["team"] == "axis")
		self.enemyleftnum.color = (0.067, 0.859, 0.125); // Allied Color
	else
		self.enemyleftnum.color = (1, 0.102, 0.078); // Axis Color
		

	while(self.pers["team"] != "spectator")
	{
		if (self.pers["team"] == "axis")
		{
			teammates = level.axis_alive;
			enemy = level.allies_alive;
		}
		else
		{
			teammates = level.allies_alive;
			enemy = level.axis_alive;
		}
		
		self.teamleftnum setValue(teammates);
		self.enemyleftnum setValue(enemy);
		
		level waittill("update_playersleft");
		wait level.fps_multiplier * .1;
	}
}


setup_specdisplay()
{
	if (!level.hud_playersleft)
		return;

	if (level.gametype == "ctf")
	{
		self thread setup_playerdisplay_ctf();
		return;
	}

	self endon("disconnect");
	self endon("stop_alive_scanning");
	
	// Allies Left
	self.alliesleft =  newClientHudElem(self);
	self.alliesleft.x = 2;
	self.alliesleft.y = 478;
	self.alliesleft.alignX = "left";
	self.alliesleft.alignY = "bottom";
	self.alliesleft.alpha = 1;
	self.alliesleft setShader(game["plyrlft_allies"], 12, 12);
	
	self.alliesleftnum =  newClientHudElem(self);
	self.alliesleftnum.x = 15;
	self.alliesleftnum.y = 478;
	self.alliesleftnum.alignX = "left";
	self.alliesleftnum.alignY = "bottom";
	self.alliesleftnum.fontScale = 1.2;
	self.alliesleftnum.color = (0.067, 0.859, 0.125); // Allied Color
	self.alliesleftnum.alpha = 1;

	// Axis Left
	self.axisleft =  newClientHudElem(self);
	self.axisleft.x = 95;
	self.axisleft.y = 478;
	self.axisleft.alignX = "left";
	self.axisleft.alignY = "bottom";
	self.axisleft.alpha = 1;
	self.axisleft setShader(game["plyrlft_axis"], 12, 12);
	
	self.axisleftnum =  newClientHudElem(self);
	self.axisleftnum.x = 108;
	self.axisleftnum.y = 478;
	self.axisleftnum.alignX = "left";
	self.axisleftnum.alignY = "bottom";
	self.axisleftnum.fontScale = 1.3;
	self.axisleftnum.color = (1, 0.102, 0.078); // Axis Color
	self.axisleftnum.alpha = 1;

	
	while(self.pers["team"] == "spectator")
	{
		self.axisleftnum setValue(level.axis_alive);
		self.alliesleftnum setValue(level.allies_alive);

		level waittill("update_playersleft");
		wait level.fps_multiplier * .1;
	}
}


players_left()
{
	while (1)
	{
		level waittill("update_playersleft");

		allied_alive = 0;
		axis_alive = 0;
		
		players = getentarray("player", "classname");
		for(i = 0; i < players.size; i++)
		{
			player = players[i];
			
			if(isDefined(player.pers["team"]) && player.pers["team"] != "spectator" && player.sessionstate == "playing")
			{

				if(player.pers["team"] == "allies")
					allied_alive++;
				else if(player.pers["team"] == "axis")
					axis_alive++;
			}
		}

		level.axis_alive = axis_alive;
		level.allies_alive = allied_alive;
	}
}

setup_playerdisplay_ctf()
{
	//Debug
	if (self.name == "bot0" ||
		self.name == "bot1" ||
		self.name == "bot2" ||
		self.name == "bot3" ||
		self.name == "bot4" ||
		self.name == "bot5" ||
		self.name == "bot6" ||
		self.name == "bot7" ||
		self.name == "bot8" ||
		self.name == "bot9")
	{
		return;
	}

	self endon("disconnect");
	self endon("stop_alive_scanning");

	// Team Left
	self.teamleft =  newClientHudElem(self);
	self.teamleft.x = 2;
	self.teamleft.y = 478;
	self.teamleft.alignX = "left";
	self.teamleft.alignY = "bottom";
	self.teamleft.alpha = 1;
	if (self.pers["team"] == "axis")
		self.teamleft setShader(game["plyrlft_axis"], 12, 12);
	else
		self.teamleft setShader(game["plyrlft_allies"], 12, 12);
	
	self.teamleftnum = newClientHudElem(self);
	self.teamleftnum.x = 15;
	self.teamleftnum.y = 478;
	self.teamleftnum.alignX = "left";
	self.teamleftnum.alignY = "bottom";
	self.teamleftnum.fontScale = 1.3;
	self.teamleftnum.alpha = 1;
	if (self.pers["team"] == "axis")
		self.teamleftnum.color = (1, 0.102, 0.078); // Axis Color
	else
		self.teamleftnum.color = (0.067, 0.859, 0.125); // Allied Color

		
	// Enemy Left
	self.enemyleft =  newClientHudElem(self);
	self.enemyleft.x = 95;
	self.enemyleft.y = 478;
	self.enemyleft.alignX = "left";
	self.enemyleft.alignY = "bottom";
	self.enemyleft.alpha = 1;
	if (self.pers["team"] == "allies")
		self.enemyleft setShader(game["plyrlft_axis"], 12, 12);
	else
		self.enemyleft setShader(game["plyrlft_allies"], 12, 12);

	self.enemyleftnum =  newClientHudElem(self);
	self.enemyleftnum.x = 108;
	self.enemyleftnum.y = 478;
	self.enemyleftnum.alignX = "left";
	self.enemyleftnum.alignY = "bottom";
	self.enemyleftnum.fontScale = 1.3;
	self.enemyleftnum.alpha = 1;
	if (self.pers["team"] == "axis")
		self.enemyleftnum.color = (0.067, 0.859, 0.125); // Allied Color
	else
		self.enemyleftnum.color = (1, 0.102, 0.078); // Axis Color

	while (1)
	{
		if (self.pers["team"] == "spectator" || self.pers["team"] == "axis")
		{
			teammates = level.axis_alive;
			enemy = level.allies_alive;
		}
		else
		{
			teammates = level.allies_alive;
			enemy = level.axis_alive;
		}

		self.teamleftnum setValue(teammates);
		self.enemyleftnum setValue(enemy);

		level waittill("update_playersleft");
		wait level.fps_multiplier * .1;
	}
}